package org.mirrentools.attendance.console.router;

import org.mirrentools.attendance.console.service.ReportService;

import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.sql.assist.SQLExecute;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;

/**
 * Report的接口服务
 *
 * @author
 */
public class ReportRouter {
  /**
   * 日志工具
   */
  private final Logger LOG = LoggerFactory.getLogger(this.getClass());
  /**
   * 数据服务接口
   */
  private ReportService service;

  private ReportRouter(ReportService service) {
    super();
    this.service = service;
  }

  /**
   * 启动服务
   *
   * @param router  Router
   * @param execute 数据执行器
   */
  public static void startService(Router router, SQLExecute<JDBCClient> execute) {
    ReportRouter instance = new ReportRouter(ReportService.create(execute));
    // 工作汇报
    router.route("/api/report/put").handler(instance::put);

  }

  private void put(RoutingContext rct) {
    MultiMap params = rct.request().params();
    if (LOG.isDebugEnabled()) {
      LOG.debug("执行获取数据[ReportService.put]-->请求参数:\n" + params);
    }
    service.put(params, res -> {
      rct.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8").end(res.result().toBuffer());
    });
  }
}
